package cn.zh.system.service.impl;

import cn.zh.base.service.impl.BaseServiceImpl;
import cn.zh.system.domain.Role;
import cn.zh.system.mapper.MenuMapper;
import cn.zh.system.mapper.PermissionMapper;
import cn.zh.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zh
 * @since 2023-03-09
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    @Transactional
    public void add(Role role) {
        // 添加角色
        super.add(role);
        // 角色和菜单的关系 role-menu
        menuMapper.addMiddle(role.getId(),role.getMenus());
        // 角色和按钮的关系 role-permission
        permissionMapper.addMiddle(role.getId(),role.getPermissions());
    }

    @Override
    @Transactional
    public void update(Role role) {
        super.update(role);
        menuMapper.deleteByRoleId(role.getId());
        menuMapper.addMiddle(role.getId(),role.getMenus());
        permissionMapper.deleteByRoleId(role.getId());
        permissionMapper.addMiddle(role.getId(), role.getPermissions());
    }

    @Override
    @Transactional
    public void delete(Long id) {
        super.delete(id);
        menuMapper.deleteByRoleId(id);
        permissionMapper.deleteByRoleId(id);
    }
}
